package dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.regex.Pattern;

import com.eltima.components.ui.m;

import dao.MenuDao;
import entiey.Menu;
//import jdk.jfr.Description;
import util.CastUtil;
import util.JDBCUtil;
import util.ResultSetCreateObject;

public class MenuDaoImpl implements MenuDao,ResultSetCreateObject<Menu>{

	@Override
	public int add(Menu menu) {
		int num=JDBCUtil.executeUpdate(
				"insert into menu (foodName,spellCode,type,foodIcon,foodPrice,status,description,remark) values(?,?,?,?,?,?,?,?)",
				menu.getFoodName(), menu.getSpellCode(), menu.getType(), menu.getFoodIcon(), menu.getFoodPrice(),
				menu.getStatus(), menu.getDescription(), menu.getRemark());
		return num;
	}

	@Override
	public int delete(int id) {
		int num = JDBCUtil.executeUpdate("delete from menu where id=?", id);
		return num;
	}

	@Override
	public int update(Menu menu) {
		int num = JDBCUtil.executeUpdate(
				"update menu set foodName=?,spellCode=?,type=?,foodIcon=?,foodPrice=?,status=?,description=?,remark=? where id=?",
				menu.getFoodName(), menu.getSpellCode(), menu.getType(), menu.getFoodIcon(), menu.getFoodPrice(),
				menu.getStatus(), menu.getDescription(), menu.getRemark(),menu.getId());
		return num;
	}

	@Override
	public List<Menu> findAll() {
		List<Menu> list = JDBCUtil.executeQuery("select * from menu",this );
		return list;
	}

	@Override
	public Menu findById(int id) {
		List<Menu> list=null;
		Menu menu=null;
		try {
			list = JDBCUtil.executeQuery("select * from menu where id=?", this, id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		menu = list.get(0);
		return menu;
	}

	@Override
	public Menu create(ResultSet rs) {
		Menu menu=new Menu();
		try {
			menu.setId(rs.getInt("id"));
			menu.setFoodName(rs.getString("foodName"));
			menu.setSpellCode(rs.getString("spellCode"));
			menu.setType(rs.getInt("type"));
			menu.setFoodIcon(rs.getString("foodIcon"));
			menu.setFoodPrice(rs.getDouble("foodPrice"));
			menu.setStatus(rs.getInt("status"));
			menu.setDescription(rs.getString("description"));
			menu.setRemark(rs.getString("remark"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return menu;
	}

	@Override
	public List<Menu> findByNameAndPrice(String name, double price) {
		List<Menu> list = JDBCUtil.executeQuery("select * from menu where foodName=? and foodPrice=?", this, name,price);
		return list;
	}

	@Override
	public List<Menu> likeFind(String text) {
		String pattern="\\d+";
		int id=0;
		if(Pattern.matches(pattern, text)) {
			id=CastUtil.castInt(text);
		}
		List<Menu> list = JDBCUtil.executeQuery("select * from menu where id=? or foodName like ? or spellCode like ?", this,id,"%"+text+"%","%"+text+"%");
		return list;
	}

	@Override
	public List<Menu> findByType(int type) {
		List<Menu> list = JDBCUtil.executeQuery("select * from menu where type=?",this, type);
		return list;
	}

	@Override
	public int updateComId(Menu menu, int cid) {
		int num=JDBCUtil.executeUpdate("update menu set comID=? where id=?",cid,menu.getId());
		return num;
	}

	@Override
	public int updateComId(int cid, int mid) {
		int num=JDBCUtil.executeUpdate("update menu set comID=? where id=?",cid,mid);
		return num;
	}

	@Override
	public List<Menu> selectMenu(int cid) {
		List<Menu> list=null;
//		Menu menu=null;
		try {
			list = JDBCUtil.executeQuery("select * from menu where comID=?", this, cid);
		} catch (Exception e) {
			e.printStackTrace();
		}
//		menu = list.get(0);
		return list;
	}

	@Override
	public List<Menu> findByComId(int cid) {
		String sql="select * from menu where comID=?";
		List<Menu> list=JDBCUtil.executeQuery(sql, this,cid);
		return list;
	}

}
